﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Utils : MonoBehaviour {

	public static Vector3 CopyVector3(Vector3 v3){
		Vector3 res = new Vector3 ();
		res.x = v3.x;
		res.y = v3.y;
		res.z = v3.z;
		return res;
	}

	public static void ChangeHide(GameObject obj, bool status){
		for (int i = 0; i < obj.transform.childCount; i++) {
			ChangeHide (obj.transform.GetChild (i).gameObject, status);
		}
		Renderer r = obj.GetComponent<Renderer> ();
		if (r != null) {
			r.enabled = status;
		}
	}

	public static void ChangeHide(GameObject obj){
		for (int i = 0; i < obj.transform.childCount; i++) {
			ChangeHide (obj.transform.GetChild (i).gameObject);
		}
		Renderer r = obj.GetComponent<Renderer> ();
		if (r != null) {
			r.enabled = !r.enabled;
		}
	}

	/**
	 * 计算连带伤害
	 * 一格木墙由四小格组成，左下角是1，右下角是3，左上角是2，右上角是4，假设从左边攻击木墙，击中了2，那么1 将受到伤害
	 */ 
	public static int CalculateInvolve(int direction, int index){
		if (direction == 0 || direction == 2) {
			if (index == 4 || index == 3)
				return index - 2;
			else
				return index + 2;
		} else {
			//print ( "index=" + index );
			if (index == 2 || index == 4)
				return index - 1;
			else
				return index + 1;
		}
	}

	public static void ChangeColor(GameObject obj, Color c){
		for (int i = 0; i < obj.transform.childCount; i++) {
			ChangeColor (obj.transform.GetChild(i).gameObject, c);
		}
		Renderer r = obj.GetComponent<Renderer> ();
		if (r != null) {
			r.material.color = c;
		}
	}

	public static Color GetColor(string name) {
		Color res;
		ColorUtility.TryParseHtmlString (name, out res);
		return res;
	}

	public static bool IsEquals(float a, float b, float c){
		print ("a=" + a + ",b=" + b + ", abs = " + Mathf.Abs(a - b));
		if( Mathf.Abs(a - b) < c ) return true;
		return false;
	}

	public static void SetActive(GameObject obj, bool status){
		for (int i = 0; i < obj.transform.childCount; i++) {
			SetActive (obj.transform.GetChild (i).gameObject, status);
		}
		obj.SetActive (status);
	}
}
